package com.hualian.ticket.controller;

import com.hualian.ticket.exception.BusinessException;
import com.hualian.ticket.pojo.Ticket;
import com.hualian.ticket.service.TicketService;
import com.hualian.ticket.util.JsonResult;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @author 陈静
 * @date 2020/10/20 9:51
 */
@RestController
@RequestMapping("/tkt")
public class TicketController {
    @Resource
    TicketService ticketService =null;

    /**
     * 新建优惠券
     * @param ticket 目标优惠券
     * @return 影响行数
     * @throws BusinessException
     */
    @PostMapping("/insert")
    public Map insert(@RequestBody Ticket ticket) throws BusinessException {
        //通过
        Integer lines = ticketService.insertTicket(ticket);
        JsonResult jsonResult = new JsonResult();
        if(lines>0) {
            jsonResult.setCode("1");
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常");
        }
        return jsonResult.getValues();
    }

    @PostMapping("/getTktList")
    public Map findAllByPage(@RequestParam("page") Integer page) throws BusinessException {
        Map allByPage = ticketService.findAllByPage(page);
        JsonResult jsonResult = new JsonResult();
        if(allByPage != null){
            jsonResult.setCode("1");
            jsonResult.put("tickets",allByPage.get("tickets"));
            jsonResult.put("total",allByPage.get("total"));
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常");
        }
    return jsonResult.getValues();
    }

    @RequestMapping("/exportTktList")
    public void handle(HttpServletResponse response) throws IOException {
        // 先获取数据
        List<Ticket> tickets =  ticketService.findAll();
        // POI 实现
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("sheetName");  // 工作表的名称
        String fileName = "优惠券列表.xls";   // 文件名
        // 完美实现中文文件名的转换
        fileName = URLEncoder.encode(fileName,"utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "id", "名称", "描述","规则","获得日期","使用有效期","图片","状态"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 添加数据行:
        int rowNum = 1;
        for (Ticket ticket : tickets) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);   // 自增长变量
            dataRow.createCell(1).setCellValue(ticket.getName());
            dataRow.createCell(2).setCellValue(ticket.getDesc());
            dataRow.createCell(3).setCellValue(ticket.getRole());
            dataRow.createCell(4).setCellValue(dateFormat.format(ticket.getCollectExpire()));
            dataRow.createCell(5).setCellValue(dateFormat.format(ticket.getUseExpire()));
            dataRow.createCell(6).setCellValue(ticket.getImg());
            dataRow.createCell(7).setCellValue(ticket.getState());
            rowNum++;
        }
        //返回给前端下载
        response.setContentType("application/octet-stream");
        // 以附件形式访问
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }


}
